//
//  ViewController.m
//  BeautifulNote
//
//  Created by Apple on 2019/7/16.
//  Copyright © 2019 Maker. All rights reserved.
//

#import "BNHomeController.h"

#import "BNHomeListCell.h"
#import "BNCalendarView.h"

#import <TYCyclePagerView.h>
#import <FSCalendar.h>

#import "BNShowMoodController.h"

@interface BNHomeController () <TYCyclePagerViewDataSource, TYCyclePagerViewDelegate, BNCalendarViewDelegate>

@property (nonatomic, strong) TYCyclePagerView *pageView;
@property (nonatomic, strong) TYCyclePagerViewLayout *layout;
@property (nonatomic, strong) UILabel *weekLabel;
@property (nonatomic, strong) UILabel *yearLabel;
@property (nonatomic, strong) UIButton *calendarButton;
@property (nonatomic, strong) BNCalendarView *calendarView;
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, strong) NSArray <BNHomeModel *>*datas;

@end

@implementation BNHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = mainColor;
    [self initConstains];
    self.selectedDate = [NSDate date];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"shouldReload" object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loadData {
    [self loadDataWithYear:@"" month:@"" isLoadAll:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!self.datas.count) {
        [self loadDataWithYear:@"" month:@"" isLoadAll:YES];
    }
}

#pragma mark - click action
- (void)didClickedCalendar:(UIButton *)button {
    [self.calendarView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(ScreenHeight);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        self.calendarView.alpha = 1.0f;
        [self.view layoutIfNeeded];
    }];    
}


#pragma mark - TYCyclePagerViewDelegate
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.datas.count;
}

- (__kindof UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    BNHomeListCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"BNHomeListCell" forIndex:index];
    cell.model = self.datas[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    return self.layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index {
    BNShowMoodController *vc = [BNShowMoodController new];
    vc.model = self.datas[index];
    [self presentViewController:vc animated:YES completion:nil];
}


#pragma mark - BNCalendarViewDelegate
-(void)didSelectedDate:(NSDate *)date {
    self.selectedDate = date;
    [self.calendarView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        self.calendarView.alpha = 0.0f;
        [self.view layoutIfNeeded];
    }];
}

- (void)didClickEmptView {
    [self.calendarView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        self.calendarView.alpha = 0.0f;
        [self.view layoutIfNeeded];
    }];
}

#pragma mark - private method
- (void)loadDataWithYear:(NSString*)year month:(NSString *)month isLoadAll:(BOOL)loadAll{
    if (loadAll) {
        self.datas = [BNDBHelper selectAllHomeModels];
    }else {
        self.datas = [BNDBHelper selectHomeModelsWithYear:year month:month];
    }
    [self.pageView reloadData];
}

- (void)initConstains {
    
    [self.view addSubview:self.pageView];
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo((ScreenWidth - 20) * 1.2);
    }];
    
    [self.view addSubview:self.calendarView];
    self.calendarView.alpha = 0.0f;
    [self.calendarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.yearLabel];
    [self.yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.pageView.mas_top).mas_equalTo(-10);
        make.right.mas_equalTo(-30);
    }];
    
    [self.view addSubview:self.weekLabel];
    [self.weekLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.yearLabel.mas_top).mas_equalTo(-10);
        make.right.mas_equalTo(-30);
    }];
    
    [self.view addSubview:self.calendarButton];
    [self.calendarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.yearLabel);
        make.left.mas_equalTo(30);
        make.width.height.mas_equalTo(38);
    }];
    
}

- (NSString *)getWeekDayStringWithDate:(NSDate *)date {
    NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSInteger weeekDay = [gregorian component:NSCalendarUnitWeekday fromDate:date];
    NSString *weekString = @"周一";
    switch (weeekDay) {
        case 2:
            weekString = @"周一";
            break;
        case 3:
            weekString = @"周二";
            break;
        case 4:
            weekString = @"周三";
            break;
        case 5:
            weekString = @"周四";
            break;
        case 6:
            weekString = @"周五";
        case 7:
            weekString = @"周六";
            break;
        case 1:
            weekString = @"礼拜天";
            break;
        default:
            break;
    }
    return weekString;
}

#pragma mark - getter & setter
- (void)setSelectedDate:(NSDate *)selectedDate {
    _selectedDate = selectedDate;
    NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSInteger year = [gregorian component:NSCalendarUnitYear fromDate:selectedDate];
    NSInteger month = [gregorian component:NSCalendarUnitMonth fromDate:selectedDate];

    self.weekLabel.text = [self getWeekDayStringWithDate:selectedDate];
    self.yearLabel.text = [NSString stringWithFormat:@"%ld", year];

    [self loadDataWithYear:[NSString stringWithFormat:@"%ld", year] month:[NSString stringWithFormat:@"%ld", month] isLoadAll:NO];
}

- (TYCyclePagerViewLayout *)layout {
    if (!_layout) {
        _layout = [[TYCyclePagerViewLayout alloc] init];
        _layout.itemSize = CGSizeMake(ScreenWidth - 40, (ScreenWidth - 40) * 1.2);
        _layout.itemSpacing = 10;
        _layout.layoutType = TYCyclePagerTransformLayoutCoverflow;
        _layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    }
    return _layout;
}

- (TYCyclePagerView *)pageView {
    if (!_pageView) {
        _pageView = [[TYCyclePagerView alloc]init];
        _pageView.layer.borderWidth = 1;
        _pageView.isInfiniteLoop = YES;
        _pageView.dataSource = self;
        _pageView.delegate = self;
        _pageView.layer.borderColor = [UIColor clearColor].CGColor;
        [_pageView registerClass:[BNHomeListCell class] forCellWithReuseIdentifier:@"BNHomeListCell"];
    }
    return _pageView;
}

- (UILabel *)weekLabel {
    if (!_weekLabel) {
        _weekLabel = [UILabel new];
        _weekLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
        _weekLabel.textColor = qingseMood;
        _weekLabel.text = @"周五";
    }
    return _weekLabel;
}

- (UILabel *)yearLabel {
    if (!_yearLabel) {
        _yearLabel = [UILabel new];
        _yearLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:30];
        _yearLabel.textColor = qingseMood;
        _yearLabel.text = @"2019";
    }
    return _yearLabel;
}

- (BNCalendarView *)calendarView {
    if (!_calendarView) {
        _calendarView = [[BNCalendarView alloc] init];
        _calendarView.delegate = self;
    }
    return _calendarView;
}

- (UIButton *)calendarButton {
    if (!_calendarButton) {
        _calendarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_calendarButton setImage:[UIImage imageNamed:@"rili"] forState:UIControlStateNormal];
        [_calendarButton addTarget:self action:@selector(didClickedCalendar:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _calendarButton;
}

@end
